<template>
  <CustomDialog :drag-dialog="dialogInfo">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="160px">
      <el-form-item label="名称" prop="busMainData.name">
        <el-input v-model="form.busMainData.name" />
      </el-form-item>
      <el-form-item label="IP" prop="busMainData.ip">
        <el-input v-model="form.busMainData.ip" />
      </el-form-item>
      <el-form-item label="端口号" prop="busMainData.port">
        <el-input-number v-model.number="form.busMainData.port" :min="1" controls-position="right" style="width: 120px;"/>
      </el-form-item>
      <el-form-item label="用户" prop="busMainData.user">
        <el-input v-model="form.busMainData.user"/>
      </el-form-item>
      <el-form-item label="密码" prop="busMainData.password">
        <el-input v-model="form.busMainData.password" type="password" auto-complete="off">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon" style="height: 39px;width: 13px;margin-left: 2px;" />
        </el-input>
      </el-form-item>
      <el-form-item label="内部IP地址" prop="busMainData.attribute1">
        <el-input v-model="form.busMainData.attribute1" placeholder="输入类似地址:\\192.168.0.1\tmp"/>
      </el-form-item>
      <el-form-item label="默认标识" prop="busMainData.defaultFlag">
        <el-switch v-model="form.busMainData.defaultFlag" class="drawer-switch" active-value="Y" inactive-value="N"/>
      </el-form-item>
    </el-form>
    <div v-if="form.busPageInfo.editable" slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','FTPCONFIG_ALL','FTPCONFIG_CREATE']" :loading="loading" type="primary" @click="doSubmit">保存</el-button>
    </div>
  </CustomDialog>
</template>

<script>
import { addOrEdit, viewMainData, toAdd, toCopy } from '@/api/system/ftpConfig'
export default {
  components: { },
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      isAdd: false,
      loading: false,
      style: 'width: 184px',
      moulePartEdit: false,
      dialogInfo: {
        name: 'ftpConfigFormDialog',
        title: 'FTP配置维护',
        fullScreen: false,
        width: 500,
        height: 80
      },
      form: {
        busMainData: {
          id: '',
          name: '',
          enableFlag: 'Y'
        },
        busPageInfo: {
        }
      },
      rules: {
        'busMainData.name': [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
        ],
        'busMainData.ip': [
          { required: true, message: '请输入电极编号', trigger: 'blur' },
          { min: 7, max: 15, message: '长度在 7 到 15 个字符', trigger: 'blur' }
        ],
        'busMainData.port': [
          { required: true, message: '请输入端口号', trigger: 'blur' }
        ],
        'busMainData.user': [
          { required: true, message: '请输入用户', trigger: 'blur' },
          { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
        ],
        'busMainData.password': [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    displayDialog() {
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.show(name)
      })
    },
    hiddenDialog() {
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.hide(name)
      })
    },
    addData() {
      this.isAdd = true
      const data = { paramBean: {}}
      toAdd(data).then(res => {
        this.displayDialog()
        this.form = res.data
      }).catch(err => {
        console.log(err)
      })
    },
    copyData(mainId) {
      this.isAdd = true
      const data = { paramBean: { id: mainId }}
      toCopy(data).then(res => {
        this.displayDialog()
        this.form = res.data
      }).catch(err => {
        console.log(err)
      })
    },
    viewMainData(id) {
      this.isAdd = false
      const data = { paramBean: { id: id }}
      viewMainData(data).then(res => {
        this.displayDialog()
        this.form = res.data
      })
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.hiddenDialog()
      this.$refs['form'].resetFields()
    }
  }
}
</script>
